<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <a-row :gutter="10">
        <a-form :layout="formLayout" labelAlign="left" :form="searchForm" @submit="handleSubmit">
          <a-col :span="6">
            <a-form-item label="学员姓名" :label-col="labelCol" :wrapper-col="wrapperCol">
              <a-input
                placeholder="请输入学员姓名"
                v-decorator="['full_name']"
              />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item label="身份证号" :label-col="labelCol" :wrapper-col="wrapperCol">
              <a-input
                placeholder="请输入身份证号"
                v-decorator="['idnumber']"
              />
            </a-form-item>
          </a-col>
          <a-col :span="6">
            <a-form-item :label-col="labelCol" :wrapper-col="{ style: 'width: 100%;float:left' }">
              <a-button type="primary" html-type="submit" icon="search">点击搜索</a-button>
              <a-button style="margin-left:10px" @click="handleReset">重置表单</a-button>
            </a-form-item>
          </a-col>
        </a-form>
      </a-row>
    </div>
    <s-table
      ref="table"
      rowKey="student_id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15"
      tableLayout="auto"
      :scroll="{x:true}"
    >
      <div slot="accumulate_learn" slot-scope="text, item">
        <a style="font-weight: 600;font-size: 20px;">{{ text }}</a>
      </div>
      <div slot="last_avatar" slot-scope="text, item">
        <a :href="text" target="_blank"><img :src="text" style="height:60px"></a>
      </div>
      <div class="actions" slot="action" slot-scope="text, item">
        <template>
          <a v-action:delete @click="handleDelete(item)">删除</a>
        </template>
      </div>
    </s-table>
    <ExamLogModel
      ref="ExamLogModel"
    />
  </a-card>
</template>
<script>
import * as Api from '@/api/user'
import { STable, UploadImage } from '@/components'
import ExamLogModel from './examLogModel/Index'

const columns = [
  // {
  //   title: '用户ID',
  //   dataIndex: 'user_id'
  // },
  {
    title: '序号',
    dataIndex: 'no',
    key: 'no',
    customRender: (text, record, index) => `${index + 1}`
  },
  // {
  //   title: '最新头像',
  //   dataIndex: 'last_avatar.external_url',
  //   scopedSlots: { customRender: 'last_avatar' }
  // },
  // {
  //   title: '承诺签名',
  //   dataIndex: 'comsign_file.external_url',
  //   scopedSlots: { customRender: 'sign_url' }
  // },
  // {
  //   title: '交卷签名',
  //   dataIndex: 'examsign_file.external_url',
  //   scopedSlots: { customRender: 'sign_url' }
  // },
  {
    title: '学员姓名',
    dataIndex: 'full_name',
    align: 'center'
  },
  {
    title: '身份证号',
    align: 'center',
    dataIndex: 'idnumber'
  },
  // {
  //   title: '手机号码',
  //   align: 'center',
  //   dataIndex: 'phone'
  // },
  {
    title: '创建时间',
    dataIndex: 'create_time'
  },
  {
    fixed: 'right',
    title: '操作',
    dataIndex: 'action',
    width: '100px',
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  name: 'Index',
  components: {
    STable,
    UploadImage,
    ExamLogModel
  },
  data () {
    return {
      currentUser: this.$store.getters.userInfo,
      searchForm: this.$form.createForm(this),
      formLayout: 'horizontal',
      labelCol: {
        style: 'width: 70px;float:left;margin-right:10px;white-space: nowrap;\n' +
          'text-overflow: ellipsis;\n' +
          '-o-text-overflow: ellipsis;\n' +
          'overflow: hidden;'
      },
      wrapperCol: { style: 'width: calc( 95% - 70px );float:left' },
      codeUrl: '',
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns,
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.list({ ...param, ...this.queryParam })
          .then(response => {
            response.data.list.data.forEach(item => {
              item.is_edit = false
            })
            return response.data.list
          })
      },
      userList: []
    }
  },
  created () {
  },
  methods: {
    handleExamLog (item) {
      this.$refs.ExamLogModel.open()
    },

    handleSubmit (e) {
      e.preventDefault()
      const { searchForm: { validateFields } } = this
      validateFields((errors, values) => {
        if (errors) {
          return false
        }
        this.queryParam = { ...this.queryParam, ...values }
        this.handleRefresh(true)
      })
    },
    handleReset () {
      this.searchForm.resetFields()
    },

    /**
     * 新增记录
     */
    handleAdd () {
      // this.$refs.AddForm.add()
    },

    /**
     * 编辑记录
     */
    handleEdit (item) {
      // this.$refs.EditForm.edit(item)
    },

    handleCancel (item) {
      Api.edit({ studentId: item['student_id'], form: { is_cancel: item['is_cancel'] ? 0 : 1 } })
        .then((result) => {
          this.handleRefresh()
        })
    },
    /**
     * 删除记录
     */
    handleDelete (item) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk () {
          return Api.deleted({ studentId: item['student_id'] })
            .then((result) => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally(result => {
              modal.destroy()
            })
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      this.$refs.table.refresh(bool)
    },

    /**
     * 检索查询
     */
    onSearch () {
      this.handleRefresh(true)
    }
  }
}
</script>
